package com.haoshuang.zhouzhoubang.utils

import android.content.Context
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import com.haoshuang.zhouzhoubang.R
import com.luck.picture.lib.basic.PictureSelector
import com.luck.picture.lib.config.SelectMimeType
import com.luck.picture.lib.config.SelectModeConfig
import com.luck.picture.lib.entity.LocalMedia
import com.luck.picture.lib.interfaces.OnMediaEditInterceptListener
import com.luck.picture.lib.interfaces.OnPermissionsInterceptListener
import com.luck.picture.lib.interfaces.OnRequestPermissionListener
import com.luck.picture.lib.interfaces.OnResultCallbackListener
import com.luck.picture.lib.style.BottomNavBarStyle
import com.luck.picture.lib.style.PictureSelectorStyle
import com.luck.picture.lib.style.SelectMainStyle
import com.luck.picture.lib.style.TitleBarStyle
import com.luck.picture.lib.utils.DensityUtil


object MultiMediaChooseUtils {

    fun chooseMultiMedia(
        context: Context,
        isVideo: Boolean = false,
        isCamera: Boolean = false,
        permissionTitle: String = "权限使用说明",
        permissionContent: String = "打开相机拍照或相册选择图片",
        onChoose: (result: LocalMedia?) -> Unit
    ) {
        PictureSelector.create(context)
            .openGallery(if (isVideo) SelectMimeType.ofVideo() else SelectMimeType.ofImage())
            .setImageEngine(GlideEngine.createGlideEngine())
            .isCameraForegroundService(false)
            .setImageSpanCount(4)
            .isDisplayCamera(isCamera)
            .setSelectionMode(SelectModeConfig.SINGLE) // 单选
            .setRecordVideoMaxSecond(60) // 视频最大秒数 60秒
            .setFilterVideoMaxSecond(60)
            .isEmptyResultReturn(true)
            .isCameraRotateImage(true)
            .setPermissionsInterceptListener(object : OnPermissionsInterceptListener {
                override fun requestPermission(
                    fragment: Fragment?,
                    permissionArray: Array<out String>?,
                    call: OnRequestPermissionListener?
                ) {
                    permissionArray?.let {
                        PermissionHelper.requestPermission(
                            fragment?.context,
                            permissionTitle,
                            permissionContent,
                            *it
                        ) {
                            call?.onCall(permissionArray, it)
                        }
                    }
                }

                override fun hasPermissions(
                    fragment: Fragment?,
                    permissionArray: Array<out String>?
                ): Boolean {
                    return false
                }
            })
//            .setCompressEngine(CompressFileEngine { context, source, call ->
//                Luban.with(context)
//                    .load(source)
//                    .ignoreBy(100)
//                    .setCompressListener(object : OnNewCompressListener {
//                        override fun onStart() {
//                        }
//
//                        override fun onSuccess(source: String?, compressFile: File?) {
//                            call?.onCallback(source, compressFile?.absolutePath)
//                        }
//
//                        override fun onError(source: String?, e: Throwable?) {
//                            call?.onCallback(source, null)
//                        }
//                    }).launch()
//            })
            .forResult(object : OnResultCallbackListener<LocalMedia> {
                override fun onResult(result: ArrayList<LocalMedia>?) {
                    if (!result.isNullOrEmpty()) {
                        onChoose(result[0])
                    } else {
                        onChoose(null)
                    }
                }

                override fun onCancel() {

                }
            })
    }

    fun cameraMultiMedia(
        context: Context,
        isVideo: Boolean = false,
        permissionTitle: String = "权限使用说明",
        permissionContent: String = "打开相机拍照或录制视频",
        onChoose: (result: LocalMedia?) -> Unit
    ) {

        PictureSelector.create(context)
            .openCamera(if (isVideo) SelectMimeType.ofVideo() else SelectMimeType.ofImage())
            .isCameraForegroundService(false)
            .setRecordVideoMaxSecond(60) // 视频最大秒数 60秒
            .isCameraRotateImage(true)
            .setPermissionsInterceptListener(object : OnPermissionsInterceptListener {
                override fun requestPermission(
                    fragment: Fragment?,
                    permissionArray: Array<out String>?,
                    call: OnRequestPermissionListener?
                ) {
                    permissionArray?.let {
                        PermissionHelper.requestPermission(
                            fragment?.context,
                            permissionTitle,
                            permissionContent,
                            *it
                        ) {
                            call?.onCall(permissionArray, it)
                        }
                    }
                }

                override fun hasPermissions(
                    fragment: Fragment?,
                    permissionArray: Array<out String>?
                ): Boolean {
                    return false
                }
            })
            .forResult(object : OnResultCallbackListener<LocalMedia> {
                override fun onResult(result: ArrayList<LocalMedia>?) {
                    if (!result.isNullOrEmpty()) {
                        onChoose(result[0])
                    } else {
                        onChoose(null)
                    }
                }

                override fun onCancel() {

                }
            })


    }

    fun checkImage(
        context: Context,
        num: Int,
        permissionTitle: String = "权限使用说明",
        permissionContent: String = "打开相机拍照或相册选择图片",
        onChoose: (result: MutableList<LocalMedia>) -> Unit
    ) {
        PictureSelector.create(context)
            .openGallery(SelectMimeType.ofImage())
            .setImageEngine(GlideEngine.createGlideEngine())
            .isCameraForegroundService(false)
            .setImageSpanCount(4)
            .isDisplayCamera(true)
            .setSelectionMode(SelectModeConfig.MULTIPLE) // 单选
            .setMaxSelectNum(num)
            .setRecordVideoMaxSecond(60) // 视频最大秒数 60秒
            .setFilterVideoMaxSecond(60)
            .isCameraRotateImage(true)
            .setPermissionsInterceptListener(object : OnPermissionsInterceptListener {
                override fun requestPermission(
                    fragment: Fragment?,
                    permissionArray: Array<out String>?,
                    call: OnRequestPermissionListener?
                ) {
                    permissionArray?.let {
                        PermissionHelper.requestPermission(
                            fragment?.context,
                            permissionTitle,
                            permissionContent,
                            *it
                        ) {
                            call?.onCall(permissionArray, it)
                        }
                    }
                }

                override fun hasPermissions(
                    fragment: Fragment?,
                    permissionArray: Array<out String>?
                ): Boolean {
                    return false
                }
            })
            .forResult(object : OnResultCallbackListener<LocalMedia> {
                override fun onResult(result: ArrayList<LocalMedia>?) {
                    if (!result.isNullOrEmpty()) {
                        onChoose(result)
                    } else {
                        onChoose(mutableListOf())
                    }
                }

                override fun onCancel() {

                }
            })

    }

}